﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookVoting
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //获取用户IP
            string UserIP = Request.UserHostAddress.ToString();
            //创建Cookie对象
            HttpCookie oldCookie = Request.Cookies["userIP"];
            if(oldCookie == null)
            {
                int relust = RadioButtonList1.SelectedIndex;
                switch (relust)
                {
                    case 0:
                        count.addCount(Server.MapPath("count1.txt"));
                        break;
                    case 1:
                        count.addCount(Server.MapPath("count2.txt"));
                        break;
                    case 2:
                        count.addCount(Server.MapPath("count3.txt"));
                        break;
                    case 3:
                        count.addCount(Server.MapPath("count4.txt"));
                        break;
                    default:
                        break;
                }
                Response.Write("<script>alert('投票成功，谢谢您的参与！')</script>");
                //定义新的Cookie对象
                HttpCookie newCookie = new HttpCookie("userIP");
                newCookie.Expires = DateTime.MaxValue;
                //添加新的Cookie变量IPaddress，值为UserIP
                newCookie.Values.Add("IPaddress", UserIP);
                //将变量写入Cookie文件中
                Response.AppendCookie(newCookie);
            }
            else
            {
                //防止重复投票
                string oldIP = oldCookie.Values["IPaddress"];
                if (UserIP.Trim() == oldIP.Trim())
                {
                    Response.Write("<script>alert('一个IP地址只能投一次票，谢谢您的参与！');</script>");
                }
                else
                {
                    HttpCookie newCookie = new HttpCookie("userIP");
                    newCookie.Values.Add("IPaddress", UserIP);
                    newCookie.Expires = DateTime.MaxValue;
                    Response.AppendCookie(newCookie);
                    int relust = RadioButtonList1.SelectedIndex;
                    switch (relust)
                    {
                        case 0:
                            count.addCount(Server.MapPath("count1.txt"));
                            break;
                        case 1:
                            count.addCount(Server.MapPath("count2.txt"));
                            break;
                        case 2:
                            count.addCount(Server.MapPath("count3.txt"));
                            break;
                        case 3:
                            count.addCount(Server.MapPath("count4.txt"));
                            break;
                        default:
                            break;
                    }
                    Response.Write("<script>alert('投票成功，谢谢您的参与！')</script>");
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //跳转Default2
            Response.Redirect("Default2.aspx");
        }
    }
}